package styx.lex;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentHashMap;

import android.app.Activity;
import android.graphics.PointF;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

public class CutCutActivity extends Activity {
	public static final int FADE_PATH = 1;

	public View currentView;
	public StartingView startingView;
	public CuttingView cuttingView;
	public ScoreView scoreView;

	public Map<String, ArrayList<PointF>> _pathMap = new ConcurrentHashMap<String, ArrayList<PointF>>();

	public Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case FADE_PATH:
				synchronized (_pathMap) {
					Iterator<Entry<String, ArrayList<PointF>>> iter = _pathMap
							.entrySet().iterator();
					while (iter.hasNext()) {
						Entry<String, ArrayList<PointF>> entry = iter.next();
						String key = entry.getKey();
						ArrayList<PointF> arr = entry.getValue();

						if (arr.size() > 0)
							arr.remove(0);

						if (arr.size() == 0)
							_pathMap.remove(key);
					}
				}
				break;
			default:
				break;
			}
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		new Timer().schedule(new TimerTask() {
			@Override
			public void run() {
				handler.sendEmptyMessage(FADE_PATH);
			}
		}, 1000, 20);

		startingView = new StartingView(this);
		scoreView = new ScoreView(this);
		cuttingView = new CuttingView(this);
		// GestureView view = new GestureView(this, null);
		// PaintTestView view = new PaintTestView(this);
		// TestView view = new TestView(this, null);

		currentView = startingView;
		setContentView(currentView);
		currentView.requestFocusFromTouch();
		currentView.setFocusableInTouchMode(true);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		int action = event.getActionMasked();
		long downTime = event.getDownTime();
		
		ArrayList<PointF> arr;
		if (action == MotionEvent.ACTION_DOWN
				|| action == MotionEvent.ACTION_POINTER_DOWN) {
			int ptIndex = event.getActionIndex();
			int pointerId = event.getPointerId(ptIndex);
			String key = "" + pointerId + "-" + downTime;
			synchronized (_pathMap) {
				if (!_pathMap.containsKey(key)) {
					arr = new ArrayList<PointF>();
					_pathMap.put(key, arr);
				} else {
					arr = _pathMap.get(key);
				}
				arr.add(new PointF(event.getX(ptIndex), event.getY(ptIndex)));
			}
		} else if (action == MotionEvent.ACTION_MOVE) {
			int pointerCount = event.getPointerCount();
			synchronized (_pathMap) {
				for (int idx = 0; idx < pointerCount; idx++) {
					int pointerId = event.getPointerId(idx);
					String key = "" + pointerId + "-" + downTime;
					
					if (!_pathMap.containsKey(key)) {
						arr = new ArrayList<PointF>();
						_pathMap.put(key, arr);
					} else {
						arr = _pathMap.get(key);
					}
					
					int hisCount = event.getHistorySize();
					for (int i = 0; i < hisCount; i++) {
						arr.add(new PointF(event.getHistoricalX(idx, i), event.getHistoricalY(idx, i)));
					}
					arr.add(new PointF(event.getX(idx), event.getX(idx)));
				}
			}
		} else if (action == MotionEvent.ACTION_UP
				|| action == MotionEvent.ACTION_POINTER_UP) {

		}

		return true;
	}
}
